//
//  RHMainTabBarController.swift
//  微博新浪
//
//  Created by hezerong on 16/6/4.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit


// Class 里面只放生命周期的方法， 构造方法，属性
class RHMainTabBarController: UITabBarController {
    
    
   

    //懒加载加号按钮
  private  lazy var composeButton: UIButton = UIButton(titlt: "", image: "tabbar_compose_icon_add", backGroundImage: "tabbar_compose_button", target: self, action: "compose:", event: .TouchUpInside )
    
    var isNewFeature:Bool {
        
       return NSBundle.checkIfNewVersion()
    }
   

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        print("\(NSBundle.checkIfNewVersion)")
        
        //调用设置数据方法
        setUpData()
        addPlusButtonInTheMiddelOfTheTabBar()
        
        //设置tabbar上边的样式
        setTarBar()
        
        
        //加载新特性或欢迎页
        showsNewFeatureViewOrWelcomeView()
        
        

    
    }
    
    //把占位按钮移动到视图上面
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    
        //tabBar的subviewshi 在viewwillAppear才创建的，定制tabBar时，需要将定制的subview移动到最上层
        tabBar.bringSubviewToFront(composeButton)
    }
    
    //设置tabbar上边的样式
   private func setTarBar(){

    //设置那个图片
    tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
    tabBar.shadowImage = UIImage.createDotImage(UIColor(white: 0.9, alpha: 1.0))
        
    }
    
    
    
    //设置tabbar中间的加号按钮
  private  func addPlusButtonInTheMiddelOfTheTabBar(){
        
        //添加按钮到tabbar
        self.tabBar.addSubview(composeButton)
    
        //设置按钮背景图片
//        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState:.Normal)
//        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState:.Highlighted)
//        
//        //设置按钮的图片
//        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
//        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState:.Highlighted)
        
        //设置按钮的frame值
        composeButton.frame = CGRectInset(tabBar.bounds, tabBar.bounds.size.width/5*2, 0)
        
        composeButton.userInteractionEnabled = true 
        
     
    
    }
    
    

    //设置数据（子控制器）
  private  func setUpData(){
    
      
        //        let HomeTVC = RHHomeTableViewController()
        //        HomeTVC.tabBarItem.image = UIImage(named: "tabbar_home")
        //        HomeTVC.title = "首页"
        //        //为tabbar控制器添加子控制器
        //       addChildViewController(UINavigationController(rootViewController: HomeTVC))
        //首页
        settingChildViewController(RHHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        //消息
        settingChildViewController(RHMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        
        //占位控制器
        settingChildViewController(UIViewController(), imageName: "", title: "")
        
        //发现
        settingChildViewController(RHDiscoveryTableViewController(), imageName: "tabbar_discover", title: "发现")
        //我
        settingChildViewController(RHProfileTableViewController(), imageName:  "tabbar_profile", title: "我")   
    }

    // 创建子控制器
    private func settingChildViewController(controller:UIViewController , imageName:String , title:String){
    
        controller.tabBarItem.image = UIImage(named: imageName)// tabbar图片
        
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) //设置选中图片的颜色
        
        controller.title = title //tabbar标题
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected) //设置文字颜色为橙色
        
        addChildViewController(UINavigationController(rootViewController: controller))
        
    }
    
    
    //按钮的发布方法   
 @objc private  func compose(button:UIButton){
    
       print("发布方法")
    
    let postVC = RHPostViewController()
    
    let navVC = UINavigationController(rootViewController: postVC)
    
    presentViewController(navVC, animated: true, completion: nil)
    
    
    }

}

extension RHMainTabBarController{
    func showsNewFeatureViewOrWelcomeView(){
    
//        print(UserAccount.sharedUserAccunt.isLogin)
         //判断用户是否登录
        if UserAccount.sharedUserAccunt.isLogin{
            
        
          
            //判断是否有新特性页
            if isNewFeature{
             
                let newFeatureView = RHNewFeatureView()
                view.addSubview(newFeatureView)
               
            }else{
            
                let welcomeView = RHWelcomeView()
                view.addSubview(welcomeView)
            }
        
        }
    
    }

}
